﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xiaov.SmartWeb.Infrastructure.Cache;
using Xiaov.SmartWeb.Model.ActiveRecord;
using Xiaov.SmartWeb.Infrastructure.ExtMethod;

namespace Xiaov.SmartWeb.Model.Statistical
{
    public class PLSql
    {
        public string Tn { get; set; }

        public string Filter { get; set; }

        public string XmlPath { get; set; }

        public string XmlNode { get;set;}
        
        public GridConfig Config { get; set; }

        public S_PM_USER ViewUser { get; set; }

        public List<SSEC.DataBaseDict.Field> DataTableFieldList { get; set; }

        public string ColumnTypeString { get; set; }

        public List<SSEC.DataBaseDict.Field> TableFieldList { get; set; }

        public string GridColumns { get; set; }

        /// <summary>
        ///  初始化
        /// </summary>
        /// <param name="tn">tablename</param>
        /// <param name="xmlpath">xml路径</param>
        /// <param name="xmlnode">配置节点</param>
        public PLSql(string tn,string xmlpath,string xmlnode,string filter) 
        {
            Tn = tn;
            XmlPath = xmlpath;
            XmlNode = xmlnode;
            Filter = filter;
            Init();
        }

     

        #region 私有方法

        private void Init()
        {
            //获取数据类型
            TableFieldList = SSEC.DataBaseDict.DictFactroy.Instance().GetFields(Tn);
            //获取表头配置 使用缓存
            string cachekey = "Model.Statistical.PLSql.Init.Config." + "_" + Tn;
            Config = (GridConfig)CacheFactroy.Instance().GetCache(cachekey);
            if (Config == null)
            {
                Config = new GridConfig(XmlNode, XmlPath);//加载配置文件       
                CacheFactroy.Instance().SetCache(cachekey, Config);
            }
            //数据类型
            DataTableFieldList = GridData.GetFileds("select " + Config.SelectField + " from " + Tn + " t where rownum=1");
            List<string> list = DataTableFieldList.Select(s => s.Type).ToList();
            ColumnTypeString = list.ToJoinString('|');
            //EasyUi GridColumn Setting
            GridColumns = Get_GridClolumn();
        }

        private string Get_GridClolumn()
        {
            //使用节点缓存
            string cachekey = "Model.Statistical.PLSql.Get_GridColumn_" + Tn;

            string script = (string)Xiaov.SmartWeb.Infrastructure.Cache.CacheFactroy.Instance().GetCache(cachekey);

            if (string.IsNullOrEmpty(script))
            {
                #region 获取表头字符串信息

                //决定是几层复杂表
                List<StringBuilder> ListColumnString = new List<StringBuilder>();

                //将所有配置还原为List
                List<GridColumnConfig> list = new List<GridColumnConfig>();
                string[] titles = Config.HeaderTitleName.Split(',');
                string[] titles_xy = Config.HeaderTitleXY.Split('|');
                string[] columns = Config.HeaderColumnName.Split(',');
                string[] columns_xy = Config.HeaderColumnXY.Split('|');

                for (int i = 0; i < columns.Length; i++)
                {
                    GridColumnConfig gcc = new GridColumnConfig();
                    gcc.Name = columns[i];
                    string[] t = columns_xy[i].Split(',');
                    gcc.X = t[0].ToInt32();
                    gcc.XSpan = t[1].ToInt32();
                    gcc.Y = t[2].ToInt32();
                    gcc.YSapn = t[3].ToInt32();
                    gcc.Title = false;
                    gcc.Column = true;
                    list.Add(gcc);
                }

                for (int i = 0; i < titles.Length; i++)
                {
                    if (titles[i] != "NULL" && titles[i] != "")
                    {
                        GridColumnConfig gcc = new GridColumnConfig();
                        gcc.Name = titles[i];
                        string[] t = titles_xy[i].Split(',');
                        gcc.X = t[0].ToInt32();
                        gcc.XSpan = t[1].ToInt32();
                        gcc.Y = t[2].ToInt32();
                        gcc.YSapn = t[3].ToInt32();
                        gcc.Title = true;
                        gcc.Column = false;
                        list.Add(gcc);
                    }
                }

                string YLength = Config.HeaderColumnXY.Split('|').First().Split(',').Last();
                for (int i = 0; i < YLength.ToInt32(); i++)
                {
                    StringBuilder cc = new StringBuilder("");
                    ListColumnString.Add(cc);
                }

                list = list.OrderBy(s => s.X).ToList();
                //遍历所有放入对应的层
                foreach (var q in list)
                {
                    if (q.Column)
                    {
                        ListColumnString[q.Y].Append("{ field: '" + DataTableFieldList[q.X].FieldName + "',width:100, title: '" + q.Name + "', rowspan: " + q.YSapn + ", formatter: function (val, rec) { return unescape(rec." + DataTableFieldList[q.X].FieldName + "); } },");
                    }
                    else
                    {
                        ListColumnString[q.Y].Append("{ title: '" + q.Name + "',width:100,colspan: " + q.XSpan + ", rowspan: " + q.YSapn + " },");
                    }
                }


                for (int i = 0; i < ListColumnString.Count; i++)
                {
                    script += "[" + ListColumnString[i].Remove(ListColumnString[i].ToString().Length - 1, 1) + "],";
                }

                script = script.Remove(script.Length - 1, 1);
                #endregion
                Xiaov.SmartWeb.Infrastructure.Cache.CacheFactroy.Instance().SetCache(cachekey, script);
            }

            return script;
        }

        #endregion

    }
}
